package com.api.gbhg.luoao.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.ProjectDetailsUtil;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.luoao.utils.ReimbursementDao;
import com.api.gbhg.luoao.utils.information;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.workflow.workflow.WorkflowVersion;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

/**
 * 报销列表以及报销单数据  显示
 *
 */

@Path("/Reimbursement")
public class Reimbursementlist extends BaseBean {

    private static final Logger logger = LoggerFactory.getLogger(Reimbursementlist.class);

    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public  String getlist(@Context HttpServletRequest request, @Context HttpServletResponse response, Map<String,Object> map){

        List<String> workflowlist = new ArrayList<>();

        String userFudsWorkflowid02 = getPropValue("ReimbursementWorkflowid", "YS02Workflowid");//1读取配置文件
        String userFudsWorkflowid04 = getPropValue("ReimbursementWorkflowid", "YS04Workflowid");//2读取配置文件
        String userFudsWorkflowid05 = getPropValue("ReimbursementWorkflowid", "YS05Workflowid");//3读取配置文件
        String userFudsWorkflowid06 = getPropValue("ReimbursementWorkflowid", "YS06Workflowid");//4读取配置文件
        String userFudsWorkflowid07 = getPropValue("ReimbursementWorkflowid", "YS07Workflowid");//5读取配置文件
        String userFudsWorkflowid09 = getPropValue("ReimbursementWorkflowid", "YS09Workflowid");//6读取配置文件
        String userFudsWorkflowid10 = getPropValue("ReimbursementWorkflowid", "YS10Workflowid");//7读取配置文件
        String userFudsWorkflowid11 = getPropValue("ReimbursementWorkflowid", "YS11Workflowid");//8读取配置文件
        String userFudsWorkflowid12 = getPropValue("ReimbursementWorkflowid", "YS12Workflowid");//9读取配置文件
        String userFudsWorkflowid13 = getPropValue("ReimbursementWorkflowid", "YS13Workflowid");//10读取配置文件
        String userFudsWorkflowid14 = getPropValue("ReimbursementWorkflowid", "YS14Workflowid");//11读取配置文件
        String userFudsWorkflowid15 = getPropValue("ReimbursementWorkflowid", "YS15Workflowid");//12读取配置文件
        String userFudsWorkflowid16 = getPropValue("ReimbursementWorkflowid", "YS16Workflowid");//13读取配置文件
        String userFudsWorkflowid29 = getPropValue("ReimbursementWorkflowid", "YS29Workflowid");//14读取配置文件
        String userFudsWorkflowid30 = getPropValue("ReimbursementWorkflowid", "YS30Workflowid");//15读取配置文件
        String userFudsWorkflowid31 = getPropValue("ReimbursementWorkflowid", "YS31Workflowid");//16读取配置文件
        String userFudsWorkflowid32 = getPropValue("ReimbursementWorkflowid", "YS32Workflowid");//17读取配置文件
        String userFudsWorkflowid33 = getPropValue("ReimbursementWorkflowid", "YS33Workflowid");//18读取配置文件

        String userFudsWorkflowid27 = getPropValue("ReimbursementWorkflowid", "YS27Workflowid");//借款
        String userFudsWorkflowid28 = getPropValue("ReimbursementWorkflowid", "YS28Workflowid");//还款

        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid02));//0
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid04));//1
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid05));//2
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid06));//3
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid07));//4
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid09));//5
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid10));//6
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid11));//7
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid12));//8
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid13));//9
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid14));//10
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid15));//11
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid16));//12
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid29));//13
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid30));//14
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid31));//15
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid32));//16
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid33));//17

        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid27));//18
        workflowlist.add(WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid28));//19

         /*
        String BXWFTYPE = getPropValue("ReimbursementWorkflowid", "BXWFTYPE");
        RecordSet rs = new RecordSet();
        rs.execute("select id,workflowname,activeVersionID from workflow_base where isvalid=1 and workflowtype="+BXWFTYPE);
        while (rs.next()){
            workflowlist.add(rs.getString("activeVersionID"));
        }
        */

        User user = HrmUserVarify.getUser(request, response);  //得到当前用户
        int userId = user.getUID(); //得到当前用户id

        String jbrname = map.getOrDefault("jbrname","").toString();//经办人
        String dw = map.getOrDefault("dw","").toString();//单位
        String startTime = map.getOrDefault("startTime","").toString();//开始时间
        String endTime = map.getOrDefault("endTime","").toString();//结束时间
        String agent = map.getOrDefault("agent","").toString();//待办requestid
        String alreadydone  = map.getOrDefault("alreadydone","").toString();//已办requestid

        List<Map<String,Object>> list = new ArrayList<>();

        //待办
        RecordSet filter1 = ReimbursementDao.filter(agent, startTime, endTime,dw,userId,workflowlist);
        while (filter1.next()){
            Map<String, Object> mapdate = ReimbursementDao.workflowinfo(filter1.getStringNoTrim("requestid")
                    ,jbrname,filter1.getStringNoTrim("requestname"),workflowlist);
            if (!mapdate.isEmpty()){
                list.add(mapdate);
            }

        }

        //已办
        RecordSet filter2 = ReimbursementDao.filter(alreadydone, startTime, endTime,dw,userId,workflowlist);
        while (filter2.next()){
            Map<String, Object> mapdate = ReimbursementDao.workflowinfo(filter2.getStringNoTrim("requestid")
                    ,jbrname,filter2.getStringNoTrim("requestname"),workflowlist);
            if (!mapdate.isEmpty()){
                list.add(mapdate);
            }
        }


        return JSON.toJSONString(list);

    }



    //报销单显示
    @POST
    @Path("/details")
    @Produces(MediaType.APPLICATION_JSON)
    public String  from(Map<String,Object> map){
        logger.info("======进入报销单显示/details接口======");
        String userFudsWorkflowid04 = getPropValue("ReimbursementWorkflowid", "YS04Workflowid");//读取配置文件
        logger.info("读取配置文件完成，userFudsWorkflowid04:"+userFudsWorkflowid04);
        String userFudsactiveWFID04 = WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid04);
        String requestId = map.get("requestId").toString();
        System.err.println("requestId:"+requestId);
        String lcid = ProjectDetailsUtil.getFlowIdByRequestId(requestId);
        System.err.println("流程id："+lcid);
        String flowTableName = VersionFlowUtil.getFlowTableName(lcid);
        System.err.println("表名flowTableName："+flowTableName+" userFudsactiveWFID04="+userFudsactiveWFID04);
        Map<String, Object> data = information.data(requestId,flowTableName,userFudsactiveWFID04);
        logger.info("/details接口返回数据data："+data);
        return  JSON.toJSONString(data);
    }





}
